import requests
from pycparser.plyparser import parameterized
from common.log_tools import log
from data.api.env import URL


class BaseRequest:
    sess = requests.session()

    @classmethod
    def send_request(cls,**kwargs):
        try:
            rp = cls.sess.request(**kwargs)
        except Exception as e:
            log.error(e)
            raise e
        else:
            return rp



    @classmethod
    def excute_api_request(cls,request={"epic":"pinter","feature":"全融城目","story":"登录接口","method":"post","url":"/bank/api/login","headers": None,"cookies": None},
                           parametrize={"title":"输入正确的用户名和密码登录成功","data":{"userName":"admin","password":"123456"},"expect":{'code': '0', 'message': 'success'},"params": None,"json": None,"files": None}):
        log.debug(request,parametrize)
        method = request["method"]
        url= URL + request["url"]
        cookies = request["cookies"]
        headers = request["headers"]
        data = parametrize["data"]
        files = parametrize["files"]
        params = parametrize["params"]
        json = parametrize["json"]
        # log.debug(f"method={method},url={url},cookies={cookies};,headers={headers};,data={data},json={json},files={files};,params={params}")
        rp = cls.send_request(method=method, url=url, cookies=cookies, headers=headers, data=data, json=json, files=files,params=params)
        print(rp.text)
        return rp

if __name__ == '__main__':
    BaseRequest.excute_api_request()